package jml.defaultimple.util;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.Properties;

public class UFile
{
	public static String getShortFilename(String fileName) 
	{
		try
		{
		    String path = getAbsolutePath (fileName);
		    Process process = Runtime.getRuntime ().exec ("cmd /c for %I in (\"" + fileName.toUpperCase () + "\") do @echo %~fsI");
		    process.waitFor ();

		    byte[] data = new byte[65536];
		    int size = process.getInputStream ().read (data);

		    if (size > 0) path = new String (data, 0, size).replaceAll ("\\r\\n", "");

		    return path;			
		}
		catch(Exception ex)
		{
			ex.printStackTrace();
			throw new RuntimeException(ex);
		}
	}
	
	private static String getAbsolutePath(String fileName) throws IOException
	{
	    File file = new File (fileName);
	    String path = file.getAbsolutePath ();

	    if (file.exists () == false) file = new File (path);

	    path = file.getCanonicalPath ();

	    if (file.isDirectory () && (path.endsWith (File.separator) == false)) path += File.separator;

	    return path;
	}
	
	
	public static Properties readProperties(String filename)
	{
		FileInputStream fis=null;
		
		try
		{
			fis=new FileInputStream(filename);
			Properties p=new Properties();
			p.load(fis);
			
			return p;
		}
		catch(Exception ex)
		{
			ex.printStackTrace();
			throw new RuntimeException(ex);
		}
		finally
		{
			try
			{
				if(fis!=null ) fis.close();
			}
			catch(Exception e)
			{
				e.printStackTrace();
				throw new RuntimeException(e);
			}
		}
	}
	
	public static void saveProperties(String filename)
	{
		FileOutputStream fos=null;
		
		try
		{
			fos=new FileOutputStream(filename);
			Properties p=new Properties();
			p.store(fos,null);
			
		}
		catch(Exception ex)
		{
			ex.printStackTrace();
			throw new RuntimeException(ex);
		}
		finally
		{
			try
			{
				if(fos!=null ) fos.close();
			}
			catch(Exception e)
			{
				e.printStackTrace();
				throw new RuntimeException(e);
			}
		}
	}	
	
	public static FolderIterator createFolderIterator(File root)
	{
		FolderIterator fi = new FolderIterator(root);
		return fi;
	}

	public static void copy(String fullnameFrom, String fullnameTo)
	{
		FileInputStream fis = null;
		BufferedInputStream bis = null;
		FileOutputStream fos = null;
		BufferedOutputStream bos = null;

		try
		{
			bis = new BufferedInputStream(fis = new FileInputStream(fullnameFrom));			
			bos = new BufferedOutputStream(fos = new FileOutputStream(fullnameTo));

			int BUFFER_SIZE = 1024;
			byte buffer[] = new byte[BUFFER_SIZE];

			int readed = 0;
			while((readed = bis.read(buffer)) > 0)
			{
				bos.write(buffer, 0, readed);
			}

			bos.flush();

		}
		catch(Exception e)
		{
			e.printStackTrace();
			throw new RuntimeException(e);
		}
		finally
		{
			try
			{
				if(bos != null) bos.close();
				if(bis != null) bis.close();
				if(fos != null) fos.close();
				if(fis != null) fis.close();
			}
			catch(Exception e2)
			{
				e2.printStackTrace();
				throw new RuntimeException(e2);
			}
		}
	}

	public static boolean exists(String string)
	{
		File f=new File(string);
		return f.exists();
	}

	public static boolean exists(String relative, String basePath)
	{
		File f=new File(basePath+"/"+relative);
		return f.exists();
	}
	
	public static String[] getFiles(String path,final String ext)
	{
		File f = new File(path);
		return f.list(new FilenameFilter()
		{
			public boolean accept(File dir, String name)
			{
				return name.endsWith(ext);
			}
		});
	}

	public static boolean contains(File f, String file)
	{
		String lst[]=f.list();
		for(String s:lst)
		{
			if( s.equals(file) )
			{
				return true;
			}
		}
		return false;
	}

	public static int compareDate(String from, String to)
	{
		File f1 = new File(from);
		File f2 = new File(to);
		long diff = f1.lastModified()-f2.lastModified();
		
		return diff==0?0:(diff>0)?1:-1;
	}

	public static void createFolderIfNotExists(String folder)
	{
		File f = new File(folder);
		if( !f.exists() )
		{
			if( !f.mkdirs() )
			{
				System.out.println("Error al crear la carpeta: "+folder);
			}
		}
	}

	public static void createFileIfNoExists(String filename)
	{
		try
		{
			File f = new File(filename);
			if( !f.exists() )
			{
				if( !f.createNewFile() )
				{
					System.out.println("Error al crear el archivo: "+filename);
				}
			}					
		}
		catch(Exception e)
		{
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}

	public static void writeFile(String fname, String txt)
	{
		FileOutputStream fos = null;
		try
		{
			fos = new FileOutputStream(fname);
			fos.write(txt.getBytes());
		}
		catch(Exception e)
		{
			e.printStackTrace();
			throw new RuntimeException(e);
		}
		finally
		{
			try
			{
				if( fos!=null ) fos.close();
			}
			catch(Exception e2)
			{
				e2.printStackTrace();
				throw new RuntimeException(e2);
			}
		}
		
	}
	
	public static String readFile(String fname)
	{
		FileInputStream fis = null;
		try
		{
			File f = new File(fname);
			
			if( !f.exists() )
			{
				return null;
			}
			
			byte[] buff = new byte[(int)f.length()];
			
			fis = new FileInputStream(fname);
			fis.read(buff);
			
			return new String(buff);
		}
		catch(Exception e)
		{
			e.printStackTrace();
			throw new RuntimeException(e);
		}
		finally
		{
			try
			{
				if( fis!=null ) fis.close();
			}
			catch(Exception e2)
			{
				e2.printStackTrace();
				throw new RuntimeException(e2);
			}
		}
	}

	public static void removeFile(String filename)
	{
		File f = new File(filename);
		f.delete();
	}
	
	public static void main(String[] args)
	{
		String s = getShortFilename("d:\\java\\workspace\\JMusicLibrary\\My Music");
		System.out.println(s);
	}
}
